define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    ajaxOperate : ['add','edit']
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pk: 'device_id',
                sortName: 'device_id',
                queryParams:function(params) {
                    // 要显示的字段
                    params.fields = '';
                    // 要关联的拓展信息
                    params.extra = 'live,member';
                    return params;
                },
                search:false,
                commonSearch: true,
                searchFormVisible: false,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'device_id', title: __('Device id'),align:'left'},
                        {
                            field: 'live.name',
                            title: __('Live'),
                            align:'left',
                            operate:"LIKE %...%",
                            selectPage:{
                                'source': '/live/admin.live/selectpage',
                                'searchTable': 'live/live',
                                'selectToCloseList': 'false'
                            },
                            name:"live_id",
                            visible: true,
                            placeholder:"请选择"
                        },
                        {
                            field: 'member.nickname',
                            title: __('own_member'),
                            align:'left',
                            operate:'LIKE %...%',
                            selectPage:{
                                'source': '/member/member.admin/selectpage',
                                'search-table': 'member/admin',
                                'select-to-close-list': 'false',
                                'show-field':"nickname",
                                'key-field':"unionid"
                            },
                            name:"unionid",
                            placeholder:"请选择"
                        },
                        {field: 'deposit', title: __('deposit_money'),align:'left'},
                        {field: 'uuid', title: __('Uuid'),align:'left'},
                        {field: 'major', title: __('Major'),align:'left'},
                        {field: 'minor', title: __('Minor'),align:'left'},
                        {field: 'comment', title: __('Device comment'),align:'left'},
                        {field: 'status_text', title: __('Active status'),operate: false,align:'left',formatter: Controller.api.formatter.status_text},
                        {field: 'last_active_time', title: __("Last active time"),operate: false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __("Create time"),operate: false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Update time'),operate: false, formatter: Table.api.formatter.datetime,visible: false},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: Controller.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"), null, function(form, data, ret) {
                Toastr.success(data.msg ? data.msg : __('Operation completed'));
                setTimeout(function(){
                    var index = parent.Layer.getFrameIndex(window.name);
                    parent.Layer.close(index);
                    parent.location.reload();
                }, 3000);
                return false;
            });
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                status_text: function (value, row, index, custom) {
                    //渲染状态
                    var _color = 'primary';
                    if (row.status == 'normal') {
                        _color = 'success';
                    }else{
                        _color = 'danger';
                    }
                    return '<span class="text-'+ _color +'">' + row.status_text + '</span>';
                },
                operate: function (value, row, index) {
                    var _operate = '';
                        _operate += '<a href="javascript:;" class="btn btn-success btn-bind btn-xs">' + __('Bind device') + '</a>&nbsp;';
                    return _operate;
                },
            },
            events: {
                operate: $.extend({
                    'click .btn-bind': function (e, value, row, index) {
                        Backend.api.open('/wechat/shakearound/device/edit/device_id/' + row['device_id'], __('Bind device'));
                    }
                }, Table.api.events.operate)
            }
        }
    }
    return Controller;
});